/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/


#include "PlatformUtils-jni.h"
//--------------------------------------------------------------------------------
static JavaVM *gJavaVM;
static jclass cls;
static jmethodID mid;
static jobject jobj;
static std::string gcallback_str;
const char *kInterfacePath = "com/igs/muziapp/utils/PlatformUtils";
//--------------------------------------------------------------------------------
void onHttpRequestCompleted(char *jsonData)
{  
	JNIEnv* env;

	bool isAttached = false;

	if(gJavaVM->GetEnv((void **)&env,JNI_VERSION_1_4) != JNI_OK)
	{
		if(gJavaVM->AttachCurrentThread(&env,NULL) != 0)
		{
			LOGD("gJavaVM->AttachCurrentThread Fail!");			
		}
		isAttached = true;
	}

	jclass interfaceClass = env->GetObjectClass(jobj);
	if(interfaceClass != 0)
	{
		jmethodID method = env->GetMethodID(interfaceClass,gcallback_str.c_str(),"(Ljava/lang/String;)V");
		if(method != 0)
		{
			env->CallVoidMethod(jobj, method, env->NewStringUTF(jsonData));
		}
	}

	if(isAttached)
		gJavaVM->DetachCurrentThread();

}


JNIEXPORT void JNICALL Java_com_example_ndktest_PlatformUtils_Post(JNIEnv *env,jobject obj,jstring ip_str,jstring cmd_str,jstring data_str,jint type_int,jint timeout_int,jstring callback_str)
{

	const char *cip_str = env->GetStringUTFChars(ip_str,NULL);
	const char *ccmd_str = env->GetStringUTFChars(cmd_str,NULL);
	const char *cdata_str = env->GetStringUTFChars(data_str,NULL);
	int type = type_int;
	int timeout = timeout_int;
	const char *ccallback_str = env->GetStringUTFChars(callback_str,NULL);
	gcallback_str = ccallback_str;

	PLATFORM_UTILS::Post((char*)cip_str,(char*)ccmd_str,(char*)cdata_str, type,timeout,onHttpRequestCompleted);

	env->ReleaseStringUTFChars(ip_str, cip_str);
	env->ReleaseStringUTFChars(cmd_str, ccmd_str);
	env->ReleaseStringUTFChars(data_str, cdata_str);
	env->ReleaseStringUTFChars(callback_str, ccallback_str);

}

JNIEXPORT void JNICALL Java_com_example_ndktest_PlatformUtils_Get(JNIEnv *env,jobject obj,jstring ip_str,jstring cmd_str,jstring data_str,jint type_int,jint timeout_int,jstring callback_str,jstring control_str)
{

	const char *cip_str = env->GetStringUTFChars(ip_str,NULL);
	const char *ccmd_str = env->GetStringUTFChars(cmd_str,NULL);
	const char *cdata_str = env->GetStringUTFChars(data_str,NULL);
	int type = type_int;
	int timeout = timeout_int;
	const char *ccallback_str = env->GetStringUTFChars(callback_str,NULL);
	const char *ccontrol_str = env->GetStringUTFChars(control_str,NULL);
	gcallback_str = ccallback_str;

	PLATFORM_UTILS::Get((char*)cip_str,(char*)ccmd_str,(char*)cdata_str, type,timeout,onHttpRequestCompleted,(char*)ccontrol_str);

	LOGD("R_cip_str:%s",cip_str);
	LOGD("R_ccmd_str:%s",ccmd_str);
	LOGD("R_cdata_str:%s",cdata_str);
	LOGD("R_type:%d",type);
	LOGD("R_timeout:%d",timeout);
	LOGD("R_ccallback_str:%s",ccallback_str);
	LOGD("R_ccontrol_str:%s",ccontrol_str);

	env->ReleaseStringUTFChars(ip_str, cip_str);
	env->ReleaseStringUTFChars(cmd_str, ccmd_str);
	env->ReleaseStringUTFChars(data_str, cdata_str);
	env->ReleaseStringUTFChars(callback_str, ccallback_str);
	env->ReleaseStringUTFChars(control_str, ccontrol_str);

}

void *native_thread_start(void *arg)
{
	while(1)
	{
		usleep(1);
		PLATFORM_UTILS::ReceiveResponse();
	}

}


JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
{
	JNIEnv* env;
	gJavaVM = vm;


	if(vm->GetEnv((void**)&env,JNI_VERSION_1_4) != JNI_OK)
	{
		LOGD("Failed to get the environment using GetEnv()");
		return -1;
	}

	cls = env->FindClass(kInterfacePath);
	if(cls != 0)
	{
		mid = env->GetMethodID(cls,"<init>","()V");
		if(mid != 0)
		{
			jobj = env->NewObject(cls,mid);
			if(jobj != 0)
			{
				jobj = env->NewGlobalRef(jobj);
				LOGD("Init Ok");			
			}
		}

	}

	pthread_t native_thread;
	if(pthread_create(&native_thread, NULL, native_thread_start, NULL)) {
		LOGD("callVoid: failed to create a native thread");
	}
	return JNI_VERSION_1_4;
}

